<template>
  <a-layout-header
    :class="[
      fixHeader && 'ant-header-fixedHeader',
      layout === 'side'
        ? sidebarOpened
          ? 'ant-header-side-opened'
          : 'ant-header-side-closed'
        : null,
      theme,
      'global-header',
      isMobile ? 'no-pad-lef' : ''
    ]"
  >
    <div :class="['global-header-wide', layout]" id="layout">
      <router-link
        v-if="!isMobile && layout === 'head'"
        to="/"
        :class="['logo', isMobile ? null : 'pc', theme]"
      >
        <img width="32" src="static/img/logo.png" alt="" />
        <h1>{{ systemName }}</h1>
      </router-link>
      <a-icon
        v-if="layout === 'side'"
        class="trigger"
        style="line-height: 72px;"
        :type="collapsed ? 'menu-unfold' : 'menu-fold'"
        @click="toggleCollapse"
      />
      <div v-if="layout === 'head'" class="global-header-menu">
        <i-menu
          style="height: 64px; line-height: 64px;"
          class="system-top-menu"
          :theme="theme"
          mode="horizontal"
          :menuData="menuData"
          @select="onSelect"
        />
      </div>
      <!-- 288 80+68+140; 464 256+68+140-->
      <div style="display: flex;justify-content: space-between;width: 100%;">
        <a-tabs
          v-if="layout === 'side' && !isMobile"
          :defaultActiveKey="defaultKey"
          :style="{
            width: collapsed ? 'calc(100vw - 288px)' : 'calc(100vw - 500px)'
          }"
          class="nav-top-menu"
          @change="changeMenu"
        >
          <a-tab-pane
            v-for="item in menu"
            :key="item.id || 1"
            v-show="item.meta.isShow"
            :tab="item.name"
          ></a-tab-pane>
        </a-tabs>
        <div
          :class="[
            layout === 'side' && !isMobile
              ? 'global-header-right-flex'
              : 'global-header-right',
            theme
          ]"
        >
          <header-avatar class="header-item" />
        </div>
      </div>
    </div>
  </a-layout-header>
</template>

<script>
import HeaderAvatar from "./HeaderAvatar";
import IMenu from "@/components/menu/menu";
import { mapState } from "vuex";

export default {
  name: "GlobalHeader",
  components: { IMenu, HeaderAvatar },
  props: ["collapsed", "menuData"],
  data() {
    return {
      menu: [],
      defaultKey: "1"
    };
  },
  computed: {
    ...mapState({
      isMobile: state => state.setting.isMobile,
      layout: state => state.setting.layout,
      systemName: state => state.setting.systemName,
      sidebarOpened: state => state.setting.sidebar.opened,
      fixHeader: state => state.setting.fixHeader,
      mapData: state => state.setting.mapData,
      fullMenu: state => state.setting.fullMenu
    }),
    theme() {
      return this.layout === "side" ? "light" : this.$store.state.setting.theme;
    }
  },
  created() {
    this.menu = JSON.parse(localStorage.getItem("USER_ROUTER"))
      .find(item => item.path === "/")
      .children.filter(v => !(v.meta.isShow === false));
    this.menu.forEach(v => {
      if (this.$route.path.match(v.path)) {
        this.defaultKey = v.id;
        this.changeMenu(v.id, true);
      }
    });
  },
  methods: {
    toggleCollapse() {
      this.$emit("toggleCollapse");
    },
    onSelect(obj) {
      console.log("onSelect", obj);
      this.$emit("menuSelect", obj);
    },
    redirect(param) {
      if (param[0].children && param[0].children.length > 0) {
        this.redirect(param[0].children);
      } else {
        this.$router.push(param[0].path);
      }
    },
    changeMenu(key, disChange) {
      if (key === "1") {
        this.$store.dispatch("setting/setMenu", { menuData: this.fullMenu });
        console.log("home");
        this.$router.push("/personal");
      } else {
        const menuData = this.mapData[key];
        this.$store.dispatch("setting/setMenu", { menuData });
        if (!disChange) {
          this.redirect(menuData);
        }
      }
    }
  }
};
</script>

<style lang="less">
#layout {
  .ant-tabs-ink-bar {
    bottom: auto;
    top: 1px;
    color: red;
  }
  .ant-tabs-bar.ant-tabs-top-bar {
    border: none;
  }
  .ant-tabs-nav-container {
    line-height: 3;
  }
}
.trigger {
  font-size: 20px;
  line-height: 72px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}
.header-item {
  padding: 0 19px;
  display: inline-block;
  height: 100%;
  cursor: pointer;
  vertical-align: middle;
  i {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.65);
  }
}
.global-header {
  padding: 0 12px 0 0;
  -webkit-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: relative;
  width: 100%;
  &.light {
    background: #fff;
  }
  &.dark {
    background: #001529;
  }
  .global-header-wide {
    display: flex;
    width: 100%;
    &.head {
      padding: 0 24px;
    }
    &.side {
    }
    .logo {
      height: 64px;
      line-height: 58px;
      vertical-align: top;
      display: inline-block;
      padding: 0 12px 0 24px;
      cursor: pointer;
      font-size: 20px;
      &.pc {
        padding: 0 12px 0 0;
        white-space: nowrap;
      }
      img {
        display: inline-block;
        vertical-align: middle;
      }
      h1 {
        display: inline-block;
        font-size: 16px;
      }
      &.dark h1 {
        color: #fff;
      }
    }
    .global-header-menu {
      display: inline-block;
    }
    .nav-top-menu {
      // flex: 1 1 1000px;
    }
    .global-header-right-flex {
      flex: 0 0 210px;
      align-self: flex-end;
      height: 64px;
      // overflow: hidden;
    }
    .global-header-right {
      position: absolute;
      right: 0;
      top: 0;
      &.dark {
        color: #fff;
        i {
          color: #fff;
        }
      }
    }
  }
}
.ant-header-fixedHeader {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 15;
  // width: 100%;
  transition: width 0.2s;

  &.ant-header-side-opened {
    width: 100%;
    padding-left: 254px;
  }

  &.ant-header-side-closed {
    width: 100%;
    padding-left: 80px;
  }
}
.no-pad-lef {
  padding-left: 0 !important;
}
</style>
